package com.me.jingdongshop.ui.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.me.common.base.BaseFragment;
import com.me.common.ui.BaseAdapterLV;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.Category;
import com.me.jingdongshop.model.bean.ListBean;
import com.me.jingdongshop.presenter.CategoryFragmentPresenter;
import com.me.jingdongshop.ui.activity.AccountCenterActivity;
import com.me.jingdongshop.ui.activity.GuideActivity;
import com.me.jingdongshop.ui.activity.HomeSearchPagerActivity;
import com.me.jingdongshop.ui.activity.MainActivity;
import com.me.jingdongshop.ui.activity.ProductlistActivity;
import com.me.jingdongshop.ui.adapter.GoodsAdapterLv;
import com.me.jingdongshop.ui.adapter.GoodsCategoryAdapter;
import com.me.jingdongshop.ui.holder.GoodsCategoryHolder;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by YL on 2017/5/4.
 */
public class CategoryFragment extends BaseFragment {


    private RecyclerView rvGoodsType;
    private ListView rightLv;
    private GoodsCategoryAdapter categoryAdapter;
    private GoodsAdapterLv goodsAdapter;
    private ListBean mListBean;
    private ArrayList<ListBean> goodsType1;
    private ArrayList<ListBean> goodsType2;
    private ArrayList<ListBean> goodsType3;
    private ArrayList<ListBean> goodsType4;
    private ArrayList<ListBean> goodsType5;
    private ArrayList<ListBean> goodsType6;
    private ArrayList<ListBean> goodsDatas;

    private RelativeLayout rtInput;





    @Override
    public int getLayoutRes() {
        return R.layout.fragment_category;
    }

    @Override
    public void initView() {

        rvGoodsType = (RecyclerView) findView(R.id.rv_goods_type);
        rightLv = (ListView) findView(R.id.lv_goods_right);
        rtInput = (RelativeLayout) findView(R.id.rt_input);

        rtInput.setOnClickListener(this);



    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        CategoryFragmentPresenter presenter = new CategoryFragmentPresenter(this);
        presenter.getCategory();

    }

    @Override
    public void onClick(View v, int id) {
        switch (id){
            case R.id.rt_input:
                Intent intent = new Intent(getContext(), HomeSearchPagerActivity.class);
                startActivity(intent);
                break;

        }
        //测试

    }

    @Override
    public void onHttpSuccess(int reqType, Message msg) {
        Category category = (Category) msg.obj;
        List<Category.CategoryBean> datas = category.getCategory();

        /*处理各级列表的数据 */
        Category.CategoryBean bean;

        ArrayList<Category.CategoryBean> goodsType0 = new ArrayList<>();

        //专区数据
        //妈妈分区的所有数据
        goodsType1 = new ArrayList<>();
        //
        goodsType2 = new ArrayList<>();
        goodsType3 = new ArrayList<>();
        goodsType4 = new ArrayList<>();
        goodsType5= new ArrayList<>();
        goodsType6 = new ArrayList<>();

        //专区右边列表的数据
        goodsDatas = new ArrayList<>();

        //妈妈专区右边列表数据

        ArrayList<Category.CategoryBean> goodsType11 = new ArrayList<>();
        ArrayList<Category.CategoryBean> goodsType12 = new ArrayList<>();
        ArrayList<Category.CategoryBean> goodsType13 = new ArrayList<>();

        //时尚专区右边列表数据

        ArrayList<Category.CategoryBean> goodsType21 = new ArrayList<>();
        ArrayList<Category.CategoryBean> goodsType22 = new ArrayList<>();

        //宝宝区右边列表数据

        ArrayList<Category.CategoryBean> goodsType31 = new ArrayList<>();
        ArrayList<Category.CategoryBean> goodsType32 = new ArrayList<>();


        //日用品区右边列表数据

        ArrayList<Category.CategoryBean> goodsType41 = new ArrayList<>();
        ArrayList<Category.CategoryBean> goodsType42 = new ArrayList<>();


        //儿童服饰区右边列表数据

        ArrayList<Category.CategoryBean> goodsType51 = new ArrayList<>();
        ArrayList<Category.CategoryBean> goodsType52 = new ArrayList<>();
        ArrayList<Category.CategoryBean> goodsType53 = new ArrayList<>();

        //儿童玩具区右边列表数据

        ArrayList<Category.CategoryBean> goodsType61 = new ArrayList<>();





        for (int i = 0; i < datas.size(); i++) {

            bean = datas.get(i);

            if (bean.getParentId() == 0) {          //左边一级列表
                goodsType0.add(bean);


                if (bean.getId()==6){           //儿童玩具专区
                    String name = null;
                    name = bean.getName();
                    goodsType61.add(bean);
                    mListBean = new ListBean(name, goodsType61);
                    goodsType6.add(mListBean);
                }


            } else if (bean.getId() == 11 || bean.getParentId() == 11
                    || bean.getId() == 12 || bean.getParentId() == 12       //妈妈专区
                    || bean.getId() == 13 || bean.getParentId() == 13) {
                if (bean.getId() == 11 || bean.getParentId() == 11) {
                    //妈妈分区的一级item的所有数据
                    String name = null;
                    if (bean.getId() == 11) {
                        name = bean.getName();
                    } else {
                        goodsType11.add(bean);
                    }
                    if (name != null) {
                        mListBean = new ListBean(name, goodsType11);
                        goodsType1.add(mListBean);
                    }

                } else if (bean.getId() == 12 || bean.getParentId() == 12) {
                    //妈妈分区的二级item的所有数据
                    String name = null;
                    if (bean.getId() == 12) {
                        name = bean.getName();
                    } else {
                        goodsType12.add(bean);
                    }
                    if (name != null) {
                        mListBean = new ListBean(name, goodsType12);
                        goodsType1.add(mListBean);
                    }

                } else if (bean.getId() == 13 || bean.getParentId() == 13) {
                    //妈妈分区的二级item的所有数据
                    String name = null;
                    if (bean.getId() == 13) {
                        name = bean.getName();
                    } else {
                        goodsType13.add(bean);
                    }
                    if (name != null) {
                        mListBean = new ListBean(name, goodsType13);
                        goodsType1.add(mListBean);
                    }
                }

            }else if (bean.getId() == 14 || bean.getParentId() == 14
                    || bean.getId() == 15 || bean.getParentId() == 15  ) {       //时尚专区

                if (bean.getId() == 14 || bean.getParentId() == 14) {
                    //妈妈分区的一级item的所有数据
                    String name = null;
                    if (bean.getId() == 14) {
                        name = bean.getName();
                    } else {
                        goodsType21.add(bean);
                    }
                    if (name != null) {
                        mListBean = new ListBean(name, goodsType21);
                        goodsType2.add(mListBean);
                    }

                } else if (bean.getId() == 15 || bean.getParentId() == 15) {
                    //妈妈分区的二级item的所有数据
                    String name = null;
                    if (bean.getId() == 15) {
                        name = bean.getName();
                    } else {
                        goodsType22.add(bean);
                    }
                    if (name != null) {
                        mListBean = new ListBean(name, goodsType22);
                        goodsType2.add(mListBean);
                    }

                }

            }else if (bean.getId() == 21 || bean.getParentId() == 21
                    || bean.getId() == 22 || bean.getParentId() == 22  ) {       //宝宝区

                if (bean.getId() == 21 || bean.getParentId() == 21) {
                    //妈妈分区的一级item的所有数据
                    String name = null;
                    if (bean.getId() == 21) {
                        name = bean.getName();
                    } else {
                        goodsType31.add(bean);
                    }
                    if (name != null) {
                        mListBean = new ListBean(name, goodsType31);
                        goodsType3.add(mListBean);
                    }

                } else if (bean.getId() == 22 || bean.getParentId() == 22) {
                    //妈妈分区的二级item的所有数据
                    String name = null;
                    if (bean.getId() == 22) {
                        name = bean.getName();
                    } else {
                        goodsType32.add(bean);
                    }
                    if (name != null) {
                        mListBean = new ListBean(name, goodsType32);
                        goodsType3.add(mListBean);
                    }

                }

            }else if (bean.getId() == 33 || bean.getParentId() == 33
                    || bean.getId() == 129 || bean.getParentId() == 129  ) {       //日用区

                if (bean.getId() == 33 || bean.getParentId() == 33) {
                    //妈妈分区的一级item的所有数据
                    String name = null;
                    if (bean.getId() == 33) {
                        name = bean.getName();
                    } else {
                        goodsType41.add(bean);
                    }
                    if (name != null) {
                        mListBean = new ListBean(name, goodsType41);
                        goodsType4.add(mListBean);
                    }

                } else if (bean.getId() == 129 || bean.getParentId() == 129) {
                    //妈妈分区的二级item的所有数据
                    String name = null;
                    if (bean.getId() == 129) {
                        name = bean.getName();
                    } else {
                        goodsType42.add(bean);
                    }
                    if (name != null) {
                        mListBean = new ListBean(name, goodsType42);
                        goodsType4.add(mListBean);
                    }

                }

            }else if (bean.getId() == 23 || bean.getParentId() == 23
                    || bean.getId() == 151 || bean.getParentId() == 151       //儿童服饰专区
                    || bean.getId() == 154 || bean.getParentId() == 154) {
                if (bean.getId() == 23 || bean.getParentId() == 23) {
                    //妈妈分区的一级item的所有数据
                    String name = null;
                    if (bean.getId() == 23) {
                        name = bean.getName();
                    } else {
                        goodsType51.add(bean);
                    }
                    if (name != null) {
                        mListBean = new ListBean(name, goodsType51);
                        goodsType5.add(mListBean);
                    }

                } else if (bean.getId() == 151 || bean.getParentId() == 151) {
                    //妈妈分区的二级item的所有数据
                    String name = null;
                    if (bean.getId() == 151) {
                        name = bean.getName();
                    } else {
                        goodsType52.add(bean);
                    }
                    if (name != null) {
                        mListBean = new ListBean(name, goodsType52);
                        goodsType5.add(mListBean);
                    }

                } else if (bean.getId() == 154 || bean.getParentId() == 154) {
                    //妈妈分区的二级item的所有数据
                    String name = null;
                    if (bean.getId() == 154) {
                        name = bean.getName();
                    } else {
                        goodsType53.add(bean);
                    }
                    if (name != null) {
                        mListBean = new ListBean(name, goodsType53);
                        goodsType5.add(mListBean);
                    }
                }

            }

        }

        //显示左侧列表

        categoryAdapter = new GoodsCategoryAdapter(mActivity, goodsType0);
        rvGoodsType.setLayoutManager(new LinearLayoutManager(mActivity));
        rvGoodsType.setAdapter(categoryAdapter);
        goodsDatas.addAll(goodsType1);

        goodsAdapter = new GoodsAdapterLv(mActivity, goodsDatas);
        rightLv.setAdapter(goodsAdapter);



        goodsAdapter.setonItemClick(new BaseAdapterLV.listering() {
            @Override
            public void onclick() {
                Intent intent = new Intent(getActivity(), ProductlistActivity.class);
                startActivity(intent);

            }
        });





    }

    public void dealEvent(int position){


        switch (position){
            case 0:
                goodsDatas.clear();
                goodsDatas.addAll(goodsType1);

                goodsAdapter.notifyDataSetChanged();
                break;
            case 1:
                goodsDatas.clear();
                goodsDatas.addAll(goodsType2);
                goodsAdapter.notifyDataSetChanged();
                break;
            case 2:
                goodsDatas.clear();
                goodsDatas.addAll(goodsType3);
                goodsAdapter.notifyDataSetChanged();
                break;
            case 3:
                goodsDatas.clear();
                goodsDatas.addAll(goodsType4);
                goodsAdapter.notifyDataSetChanged();

                break;
            case 4:

                goodsDatas.clear();
                goodsDatas.addAll(goodsType5);
                goodsAdapter.notifyDataSetChanged();
                break;
            case 5:

                goodsDatas.clear();
                goodsDatas.addAll(goodsType6);
                goodsAdapter.notifyDataSetChanged();
                break;

        }


    }


    @Override
    public void onHttpError(int reqType, String error) {

    }

}
